/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package environnement;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.TickerBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.wrapper.AgentContainer;
import java.awt.Point;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import libPackage.CommonFunction;
import graphic.World;

/**
 *
 * @author duonghau
 */
public class Environnement extends Agent{
    ArrayList<AID> agents;
    CommonFunction commonFunction;
    HashMap<String, Point> agentsPosition;
    World world;
    @Override
    protected void setup() {
        world=new World(); 
        agentsPosition=new HashMap<>();
        world.showGui(agentsPosition);        
        commonFunction=new CommonFunction();
        agents=commonFunction.getAllAgents(this);
        addBehaviour(new receivePosition());
        addBehaviour(new requestPosition(this,1000));
        addBehaviour(new updateMap(this,1000));        
    }
    
    private class updateMap extends TickerBehaviour{

        public updateMap(Agent a, long period) {
            super(a, period);
        }

        @Override
        protected void onTick() { 
//            System.out.println("Number of agent:"+agentsPosition.size());
            if (!agentsPosition.isEmpty()){
                world.showGui(agentsPosition);
            }            
        }
        
    }
    
    private class receivePosition extends CyclicBehaviour{

        @Override
        public void action() {
            MessageTemplate messageTemplate=
                    MessageTemplate.and(MessageTemplate.MatchPerformative(ACLMessage.INFORM), 
                            MessageTemplate.MatchOntology("position"));                
            ACLMessage receiveMessage=receive(messageTemplate);
            if(receiveMessage!=null){
                String[] messageContent=receiveMessage.getContent().split(" ");
                int x=Integer.parseInt(messageContent[0]);
                int y=Integer.parseInt(messageContent[1]);
                Point point=new Point(x, y);
                agentsPosition.put(receiveMessage.getSender().getLocalName(), point);
            }else{
                block();
            }               
        }
        
    }
    
    private class requestPosition extends TickerBehaviour{

        public requestPosition(Agent a, long period) {
            super(a, period);
        }

        @Override
        protected void onTick() {
            agentsPosition.clear();
            agents=commonFunction.getAllAgents(myAgent);
            if(agents.size()>0){
                ACLMessage messageReponse = new ACLMessage(ACLMessage.CFP);  
                messageReponse.setOntology("position");
                for(AID agent:agents){
                    messageReponse.addReceiver(agent);                
                    messageReponse.setContent("I want to know your position");            
                    send(messageReponse);
                }                
            }            
        }
    }
}
